package com.warm.pump.frame.util;

import java.io.UnsupportedEncodingException;

/**
 * Created by guozj on 2017/1/13.
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
    public static String stringCoverNoline(String tbname) {
        String[] tbns = tbname.split("_");
        String newtbname = "";
        for (int i = 0; i < tbns.length; i++) {
            if (i == 0) {
                newtbname += tbns[i];
                continue;
            }
            String old = tbns[i].substring(0, 1);
            newtbname += tbns[i].replaceFirst(old, old.toUpperCase());

        }
        return newtbname.substring(0, 1).toLowerCase() + newtbname.substring(1);
    }

    public static String stringCoverNolineFisrtUpper(String tbname) {
        String[] tbns = tbname.split("_");
        String newtbname = "";
        for (int i = 0; i < tbns.length; i++) {
            if (i == 0) {
                newtbname += tbns[i];
                continue;
            }
            String old = tbns[i].substring(0, 1);
            newtbname += tbns[i].replaceFirst(old, old.toUpperCase());

        }
        return newtbname.substring(0, 1).toUpperCase() + newtbname.substring(1);
    }

    /**
     * 按照指定字节长度截取字符串，防止中文被截成一半的问题
     * @param s 源字符串
     * @param length 截取的字节数
     * @return 截取后的字符串
     * @throws UnsupportedEncodingException
     */
    public static String cutString(String s, int length) throws UnsupportedEncodingException {

        byte[] bytes = s.getBytes("Unicode");
        int n = 0; // 表示当前的字节数
        int i = 2; // 要截取的字节数，从第3个字节开始
        for (; i < bytes.length && n < length; i++){
            // 奇数位置，如3、5、7等，为UCS2编码中两个字节的第二个字节
            if (i % 2 == 1){
                n++; // 在UCS2第二个字节时n加1
            }
            else{
                // 当UCS2编码的第一个字节不等于0时，该UCS2字符为汉字，一个汉字算两个字节
                if (bytes[i] != 0){
                    n++;
                }
            }
        }
        // 如果i为奇数时，处理成偶数
        if (i % 2 == 1){
            // 该UCS2字符是汉字时，去掉这个截一半的汉字
            if (bytes[i - 1] != 0){
                i = i - 1;
            }
            // 该UCS2字符是字母或数字，则保留该字符
            else{
                i = i + 1;
            }
        }

        return new String(bytes, 0, i, "Unicode");

    }


    private String subStr4Byte(String str, int len) {
        String strRet = str;
        if (str != null) {
            byte[] bt = str.getBytes();
            if (bt.length > len) {
                // 半个漢字时、byte値<0
                if (bt[len - 1] < 0) {
                    strRet = new String(bt, 0, len - 1);
                } else {
                    strRet = new String(bt, 0, len);
                }
            }
        }
        return strRet;
    }

    /**
     * 截取字符串设定长度
     * @param str
     * @param cutCount 设定长度，字节数
     * @return
     */
    public static String getSubStr(String str, int cutCount) {
        if (str == null)
            return "";

        String resultStr = "";
        char[] ch = str.toCharArray();
        int count = ch.length;
        int strBLen = str.getBytes().length;
        int temp = 0;
        for (int i = 0; i < count; i++) {
            resultStr += ch[i];
            temp = resultStr.getBytes().length;
            if (temp >= cutCount && temp < strBLen) {
                resultStr += "...";
                break;
            }
        }
        return resultStr;
    }
}
